<?php

namespace App\Http\Controllers;

use App\Models\Org;
use App\Models\Project;
use App\Models\RewardApply;
use App\Models\Team;
use App\Models\UserTeam;
use App\Models\Volunteer;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index(Request $request) {
        $user = $request->user();

        $data = [];
        if ($user->type == 5) {
            $org_list = $user->platform->org_list;
            $data['org_count'] = count($org_list);
            $data['team_count'] = Team::where('in_blacklist', '=', 0)->where('verify_status', '=', '1')->whereIn('org_id',  $org_list)->count();
            $data['volunteer_count'] = Volunteer::whereIn('org_id', $org_list)->where('in_blacklist', '=', 0)->count();
            $data['project_count'] = Project::whereIn('org_id', $org_list)->where('verify_status', '=', '1')->count();
            $data['reward_count'] = Volunteer::whereIn('org_id', $org_list)->where('in_blacklist', '=', 0)->sum('reward_time');

            $data['pending_team_count'] = Team::where('verify_status', '=', '0')->whereIn('org_id', $org_list)->count();
            $data['pending_project_count'] = Project::where('verify_status', '=', '0')->whereIn('org_id', $org_list)->count();

            $teamIds = array_map(function($item) {
                return $item['id'];
            }, Team::select('id')->whereIn('org_id', $org_list)->get()->toArray());



            $data['pending_volunteer_count'] = UserTeam::where('verify_status', '=', '0')->whereIn('team_id', $teamIds)->count()/* + UserTeam::where('verify_status', '=', '0')->whereRaw("team_id in (select id from team where org_id={$user->org_id})")->count()*/;

            $projectIds = array_map(function($item) {
                return $item['id'];
            }, Project::select('id')->whereIn('org_id', $org_list)->get()->toArray());

            $data['pending_reward_apply_count'] = RewardApply::where('verify_status', '=', '0')->whereIn('project_id', $projectIds)->count();

        } else if ($user->type == 4) {
            $data['org_count'] = Org::where('in_blacklist', '=', 0)->count();
            $data['team_count'] = Team::where('in_blacklist', '=', 0)->where('verify_status', '=', '1')->count();
            $data['volunteer_count'] = Volunteer::where('in_blacklist', '=', 0)->count();
            $data['project_count'] = Project::where('verify_status', '=', '1')->count();
            $data['reward_count'] = Volunteer::where('in_blacklist', '=', 0)->sum('reward_time');

            $data['pending_team_count'] = Team::where('verify_status', '=', '0')->count();
            $data['pending_project_count'] = Project::where('verify_status', '=', '0')->count();
            $data['pending_volunteer_count'] = UserTeam::where('verify_status', '=', '0')->count() + UserTeam::where('verify_status', '=', '0')->count();
            $data['pending_reward_apply_count'] = RewardApply::where('verify_status', '=', '0')->count();
        } else if ($user->type == 3) {
            $data['team_count'] = Team::where('in_blacklist', '=', 0)->where('verify_status', '=', '1')->where('org_id', '=', $user->org_id)->count();
            $data['volunteer_count'] = Volunteer::where('org_id', '=', $user->org_id)->where('in_blacklist', '=', 0)->count();
            $data['project_count'] = Project::where('org_id', '=', $user->org_id)->where('verify_status', '=', '1')->count();
            $data['reward_count'] = Volunteer::where('org_id', '=', $user->org_id)->where('in_blacklist', '=', 0)->sum('reward_time');

            $data['pending_team_count'] = Team::where('verify_status', '=', '0')->where('org_id', '=', $user->org->id)->count();
            $data['pending_project_count'] = Project::where('verify_status', '=', '0')->where('org_id', '=', $user->org->id)->count();

            $teamIds = array_map(function($item) {
                return $item['id'];
            }, Team::select('id')->where('org_id', '=', $user->org->id)->get()->toArray());



            $data['pending_volunteer_count'] = UserTeam::where('verify_status', '=', '0')->whereIn('team_id', $teamIds)->count()/* + UserTeam::where('verify_status', '=', '0')->whereRaw("team_id in (select id from team where org_id={$user->org_id})")->count()*/;

            $projectIds = array_map(function($item) {
                return $item['id'];
            }, Project::select('id')->where('org_id', '=', $user->org->id)->get()->toArray());

            $data['pending_reward_apply_count'] = RewardApply::where('verify_status', '=', '0')->whereIn('project_id', $projectIds)->count();
        } else {
            $data['team_count'] = Team::whereDescendantOf($user->team)->where('verify_status', '=', '1')->where('in_blacklist', '=', 0)->where('org_id', '=', $user->team->org_id)->count();

            $subTeamIds = array_map(function($item) {
                return $item['id'];
            }, Team::whereDescendantOf($user->team)->select('id')->where('org_id', '=', $user->team->org_id)->get()->toArray());

            $teamIds = array_merge([$user->team_id], $subTeamIds);

            // $data['project_count'] = Project::whereIn('team_id', $subTeamIds)->count();
            $data['project_count'] = Project::where('team_id', '=', $user->team_id)->where('verify_status', '<>', '-1')->count();
            $data['volunteer_count'] = Volunteer::join('user_team','user_team.user_id', '=', 'volunteer.user_id')->where('user_team.team_id', '=', $user->team_id)->where('user_team.verify_status', '=', '1')->where('volunteer.in_blacklist', '=', 0)->count();
            $data['reward_count'] = Volunteer::join('user_team','user_team.user_id', '=', 'volunteer.user_id')->where('user_team.team_id', '=', $user->team_id)->where('volunteer.in_blacklist', '=', 0)->sum('reward_time');

            $data['pending_team_count'] = Team::where('verify_status', '=', '0')->where('parent_id', '=', $user->team_id)->count();

            if ($user->team->auth_exempt_project) {
                $data['pending_project_count'] = Project::where('verify_status', '=', '0')->whereIn('team_id', $teamIds)->count();
            }

            $data['pending_volunteer_count'] = UserTeam::where('verify_status', '=', '0')->where('team_id', '=', $user->team->id)->count()/* + UserTeam::where('verify_status', '=', '0')->where('team_id', '=', $user->team->id)->count()*/;

            $projectIds = array_map(function($item) {
                return $item['id'];
            }, Project::select('id')->where('team_id', '=', $user->team->id)->get()->toArray());

            $data['pending_reward_apply_count'] = RewardApply::where('verify_status', '=', '0')->whereIn('project_id', $projectIds)->count();
        }

        return $this->returnJSON($data);
    }
}
